#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < ROW; i++)
	{
		int j = 0;
		for (j = 0; j < COL; j++)
		{
			board[i][j] = ' ';
		}
	}
}

void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)//行数
	{
		//数据及横向分隔符的打印
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)//最后一列数据右方的分隔符不打印
				printf("|");

		}
		printf("\n");
		//纵向分隔符的打印
		if (i < col - 1)//最后一行数据下方的分隔符不打印
		{
			int j = 0;
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < row - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋\n");
	while (1)
	{
		printf("请下棋，输入你要落子的地方:>\n");
		scanf("%d %d", &x, &y);
		if (x > 0 && x <= row && y > 0 && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("你要落子的地方被占，请重新落子\n");
		}
		else
			printf("落子地方非法，请重新落子\n");
	}

}

void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋\n");

	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (x > 0 && x < row && y > 0 && y < col)
		{
			if (board[x][y] == ' ')
			{
				board[x][y] = '#';
				break;
			}
		}
	}
}

int IsFull(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}

char Iswin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int count_Player1 = 0;//一行
	int count_Player2 = 0;//右对角线
	int count_Player3 = 0;//一列
	int count_Player4 = 0;//左对角线
	int count_Cmp1 = 0;//一行
	int count_Cmp2 = 0;
	int count_Cmp3 = 0;
	int count_Cmp4 = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == '*')//一行元素个数
				count_Player1++;
			if (board[i][j] == '#')
				count_Cmp1++;
			if (i == j)
			{
				if (board[i][j] == '*')//一行元素个数
					count_Player2++; 
				if (board[i][j] == '#')
					count_Cmp2++;
			}
		}
		if (count_Player1 == col)
		{
			return '*';
		}
		if (count_Cmp1 == col)
		{
			return '#';
		}
		count_Player1 = 0;
		count_Cmp1 = 0;
	}
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[j][i] == '*')//一行元素个数
				count_Player3++;
			if (board[j][i] == '#')
				count_Cmp3++;
			if ((i + j) == col-1)
			{
				if (board[i][j] == '*')
					count_Player4++;
				if (board[i][j] == '#')
					count_Cmp4++;
			}
		}
		if (count_Player3 == row)
		{
			return '*';
		}
			
		if (count_Cmp3 == row)
		{
			return '#';
		}
		count_Player3 = 0;
		count_Cmp3 = 0;
	}
	if (count_Player3 == row || count_Player4 == row)
	{
		return '*';
	}
	else if (count_Cmp3 == col || count_Cmp4 == col)
	{
		return '#';
	}
	else if (IsFull(board, row, col)==1)
		return 'q';
	else
		return 'c';

}

